<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户ID</label>
                        <div class="layui-input-block">
                            <input type="number" name="user_id" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">公司名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="company_name" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系人姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="contact_name" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" value="" readonly  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">电子邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="email" value="" readonly  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" value="" readonly  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">客户类型(印刷企业/光刻纸制造企业/设计公司/其他)</label>
                        <div class="layui-input-block">
                            <input type="text" name="customer_type" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">设计图ID/名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="design_id" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">设计来源(我公司官方设计库/自由设计师上传)</label>
                        <div class="layui-input-block">
                            <input type="text" name="design_source" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">设计师ID/姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="designer_name" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否已获得设计授权(1是/0否)</label>
                        <div class="layui-input-block">
                            <input type="number" name="has_authorization" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品用途</label>
                        <div class="layui-input-block">
                            <input type="text" name="product_usage" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">特殊要求说明</label>
                        <div class="layui-input-block">
                            <textarea name="special_requirements" readonly class="layui-textarea"></textarea>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">光刻尺寸长度(毫米)</label>
                        <div class="layui-input-block">
                            <input type="text" name="size_length_mm" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">光刻尺寸宽度(毫米)</label>
                        <div class="layui-input-block">
                            <input type="text" name="size_width_mm" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">模压方式(热压/UV压/不懂不清楚)</label>
                        <div class="layui-input-block">
                            <input type="text" name="pressing_method" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">镀铝方式(镀铝/镀介质/不懂不清楚)</label>
                        <div class="layui-input-block">
                            <input type="text" name="aluminum_method" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">覆膜方式(复合纸/转移纸/不懂不清楚)</label>
                        <div class="layui-input-block">
                            <input type="text" name="film_method" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">样品纸张规格长度(毫米)</label>
                        <div class="layui-input-block">
                            <input type="text" name="paper_size_length" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">样品纸张规格宽度(毫米)</label>
                        <div class="layui-input-block">
                            <input type="text" name="paper_size_width" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">纸张克重(gsm)</label>
                        <div class="layui-input-block">
                            <input type="number" name="paper_weight" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">纸张类型</label>
                        <div class="layui-input-block">
                            <input type="text" name="paper_type" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">其他纸张类型说明</label>
                        <div class="layui-input-block">
                            <input type="text" name="other_paper_type" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否指定纸张(1是/0否)</label>
                        <div class="layui-input-block">
                            <input type="number" name="is_paper_specified" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">指定纸张说明</label>
                        <div class="layui-input-block">
                            <input type="text" name="specified_paper" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">样品纸张数量(份)</label>
                        <div class="layui-input-block">
                            <input type="number" name="quantity" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">其他特殊工艺需求</label>
                        <div class="layui-input-block">
                            <textarea name="other_requirements" readonly class="layui-textarea"></textarea>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">交付地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="delivery_address" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">期望交付日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="expected_date" readonly id="expected_date"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">物流方式(普通快递/加急快递/自提)</label>
                        <div class="layui-input-block">
                            <input type="text" name="logistics_method" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">设计源文件()</label>
                        <div class="layui-input-block">
                            <input type="text" name="design_files" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">修改意见文件()</label>
                        <div class="layui-input-block">
                            <input type="text" name="modification_files" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">光刻服务费(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="service_charge" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">纸张打样费(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="paper_charge"  readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">总打样费用(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="total_charge" readonly value=""  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否需要开具发票(1是/0否)</label>
                        <div class="layui-input-block">
                            <input type="number" name="need_invoice" readonly value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">发票抬头</label>
                        <div class="layui-input-block">
                            <input type="text" name="invoice_title" readonly value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">跟进状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="follow_up_status" lay-skin="switch" lay-text="已跟进|未跟进" value="1">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/application/select" + location.search;
            const UPDATE_API = "/app/admin/application/update";

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else if (obj[0].type === "checkbox" && key === "follow_up_status") {
                                // 初始化开关状态
                                obj.prop("checked", value == 1);
                            }  else {
                                obj.attr("value", value);
                            }

                            // 特殊处理设计源文件和修改意见文件
                            if (key === "design_files" || key === "modification_files") {
                                try {
                                    let files = JSON.parse(value);
                                    let links = files.map(file => `<a href="${file}" target="_blank">${file}</a>`).join("<br>");
                                    obj.parent().html(links); // 替换父容器内容为链接
                                } catch (e) {
                                    console.error(`Error parsing ${key}:`, e);
                                }
                                return;
                            }
                        });


                        
                        // 字段 期望交付日期 expected_date
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#expected_date",
                                type: "datetime",
                            });
                        })
                        
                        // 字段 创建时间 created_at
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#created_at",
                                type: "datetime",
                            });
                        })
                        
                        // 字段 更新时间 updated_at
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#updated_at",
                                type: "datetime",
                            });
                        })
                        
                        
                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render();
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    // 获取开关的值
                    let followUpStatus = layui.$('input[name="follow_up_status"]').is(':checked') ? 1 : 0;
                    data.field["follow_up_status"] = followUpStatus;

                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
